extends Node3D
class_name ItemOrb


const GRADIENT_OFFSET_1 = 0.14
const GRADIENT_OFFSET_2 = 0.68

@export var color: Color
@export var item: Item

signal picked_up(item: Item)

@onready var gpu_particles_3d: GPUParticles3D = $GPUParticles3D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	set_particles_3d_materials_color(color)

func set_particles_3d_materials_color(color: Color) -> void:
	var draw_pass_material = gpu_particles_3d.draw_pass_1.material.duplicate()
	draw_pass_material.albedo_color = color
	gpu_particles_3d.draw_pass_1.material = draw_pass_material
	
	var process_material = gpu_particles_3d.process_material.duplicate()
	process_material.color = color
	var color_gradient = Gradient.new()
	color_gradient.add_point(GRADIENT_OFFSET_1, Color.BLACK)
	color_gradient.add_point(GRADIENT_OFFSET_2, color)
	process_material.color_initial_ramp = color_gradient
	gpu_particles_3d.process_material= process_material


func _on_area_3d_body_entered(body: Node3D) -> void:
	if body is Knight:
		(body as Knight).interaction_handler.toggle_interaction(true, self)

func _on_area_3d_body_exited(body: Node3D) -> void:
	if body is Knight:
		(body as Knight).interaction_handler.toggle_interaction(false, null)

func interact() -> void:
	picked_up.emit(item)
